<?php
/*
 * Created on May 15, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
 if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	class Adv extends MY_Controller{
		
		function __construct(){
			parent::__construct();
			
			$this->load->model('adv_model', 'adv', true);
			
			$this->load->helper('form');
			
			//$this->checkLogin();
			$this->template->set_template('admin');
        	$this->buildMenu();
		}
		
		function Adv(){
			parent::VNT_Controller();
			$this->load->library('xajax', 'category');
			$this->xajax->registerFunction(array('changePosition', &$this, 'changePosition'));
			
			$this->xajax->processRequest();
			$this->xajax_js = $this->xajax->getJavascript($this->base_url.'js/');
			$this->load->model('adv_model', 'adv', true);
			
			$this->load->helper('form');
			
			$this->checkLogin();
		}
		
		function index(){
			//echo $this->config->item('language');
			//$this->main = '<div> erer erer er </div>';
			//$this->execute();
			//$this->load->view('main', '');
			$this->view();
		}
		
        function create(){
			$data = array();
			$data['id'] = 0;
			$data['name'] = '';
			$data['link_to'] = '';
			$data['name'] = '';
			$data['position'] = '';
			$data['active'] = 1;
			$data['error_message'] = '';
			$data['legend_text'] = lang('create_adv');
			$this->main = $this->load->view('admin/adv/edit_adv', $data, true);
			$this->execute();
		}
		
		function changePosition($id, $value){
            $this->load->model('adv_model', 'adv', true);
            $adv = $this->adv->getById($id);

			if($adv){
				
                $adv->position = trim($value);

                $this->adv->update($adv);

                $objResponse = new xajaxResponse();

                //$objResponse->assign("message" , "innerHTML",'Da cap nhat. ' );

                return $objResponse;
			}
        }

		function edit($id){
			$adv = $this->adv->getById($id);
			
			if(!$adv){
				redirect('admin/adv/view');
			}
						
			$data = array();
			$data['id'] = $adv->id_adv;
			$data['name'] = $adv->name;
			$data['link_to'] = $adv->link_to;
			$data['position'] = $adv->position;
			$data['order_view'] = $adv->order_view;
			$data['active'] = $adv->active;
			$data['error_message'] = '';
			//$this->main =  $this->load->view('admin/adv/edit_adv', $data, true);
			//$this->execute();
			$this->template->parse_view('content', 'admin/adv/edit_adv', $data);
		
			$this->template->render();
		}
		
			
		function uploadFile($config){
					
			$this->load->library('upload', $config);
			$result = array('error' => false);
		
			if ( ! $this->upload->do_upload('file'))
			{
				$result['message'] = $this->upload->display_errors();
				$result['error'] = true;
			}	
			else
			{
				$result['upload_data'] = $this->upload->data();
				
			}
			
			return  $result;
		}
		
		function view(){
			
			$NUMBER_RECORD_PER_PAGE = 15;
			$this->load->library('pagination');
			//$parent = $this->uri->segment(4, 0);
			$page = (int)$this->uri->segment(4);
			$base_url = $this->base_url . 'admin/adv/view/';
			$total_rows = $this->adv->getAllCount();
			$per_page = $NUMBER_RECORD_PER_PAGE;
			$uri_segment = 4;
						
			//$this->pagination->initialize($config);
			pagination($base_url, $total_rows, $per_page , $uri_segment);
			$rows = $this->adv->getAll('ASC', $page, $NUMBER_RECORD_PER_PAGE);
			
			if($page > 0 AND count($rows)==0){
				redirect('admin/adv/view/' . ($page-NUMBER_RECORD_PER_PAGE));
				return;
			}
			
			$data = array();
			$data['rows'] = $rows;
			//$this->main = $this->load->view('admin/adv/advs_list', $data, true);
			
			//$this->execute();
			$this->template->parse_view('content', 'admin/adv/advs_list', $data);
		
			$this->template->render();
		}
		
		
		function save() {
			$id = (int) $this->input->post('txtID');
			//echo $active = $this->input->post('active');
			$adv = new stdClass();
			if($id > 0){
				$adv = $this->adv->getById($id);
				if(!$adv){
					redirect('admin/adv');
					return;
				}
			}
			else{
				$adv->id_adv = 0;
				$adv->image = '';
			}
		
			$adv->name = trim($this->input->post('txtName'));
			$adv->link_to = trim($this->input->post('txtLinkTo'));
			$adv->position = trim($this->input->post('position'));
			$adv->active = (int)($this->input->post('active'));
			//$adv->id_parent_tab = trim($this->input->post('id_parent'));
			
			$error = false;
			$error_message = '';

			//$this->load->helper(array('form', 'url'));
		
			$this->load->library('form_validation');
		
			$this->form_validation->set_rules('txtName', 'lang:name', 'required|min_length[4]|max_length[20]');
			$this->form_validation->set_rules('txtLinkTo', 'lang:link_to', 'required|prep_url');
			$this->form_validation->set_rules('position', 'lang:position_view', 'required');
		
			$attach_image = false;
			$image_upload = array('error'=> false);
			if(isset($_FILES['file']['tmp_name']) AND strlen($_FILES['file']['tmp_name']) > 0 AND $id > 0){  
				$attach_image = true;
				$image_upload = $this->upload_image('file', ADV_PATH);
				if($image_upload['error'])
					$error_message .= $image_upload['message'];
			}
			elseif($id == 0){
				$attach_image = true; // image is requeried
				$image_upload = $this->upload_image('file', ADV_PATH);
				if($image_upload['error'])
					$error_message .= $image_upload['message'];
			}
						
			if ($this->form_validation->run() == FALSE OR ($attach_image == true AND $image_upload['error']==true)){
				$data = array();
				$data['id'] = $adv->id_adv;
				$data['name'] = $adv->name;
				$data['link_to'] = $adv->link_to;
				$data['position'] = $adv->position;
				$data['active'] = $adv->active;
				$data['error_message'] = '<div style="color:red">' . validation_errors(). '<br>'. $error_message .'</div>';
			
				$data['legend_text'] = $id > 0 ?  lang('edit_adv') : lang('create_adv');
			
				if(isset($image_upload['upload_data']['file_name'])){
					@unlink(ADV_PATH . $image_upload['upload_data']['file_name']);
				}
			
				//$this->main = $this->load->view('admin/adv/edit_adv', $data, true);
				//$this->execute();
				$this->template->parse_view('content', 'admin/adv/edit_adv', $data);
				$this->template->render();
			}
		else{
			if($attach_image){
				//print_r($image_upload);
				$file_path = ADV_PATH . $image_upload['upload_data']['file_name'];
				if($id > 0 AND $adv->image!=$file_path) { 
					@unlink($adv->image);
				}
				
				$adv->image = $file_path;
                //echo "<br> My Image : " . $adv->image . "<br>";
			}
				
			if($id > 0){	
				//$adv->active = (int)($this->input->post('active1'));			
				$result = $this->adv->update($adv);
			}
			else{
				$this->adv->name = $adv->name;
				$this->adv->link_to = $adv->link_to;
				$this->adv->position = $adv->position;
				$this->adv->order_view = $this->adv->getMaxOrder() + 1;
				$this->adv->image = $adv->image;
				$this->adv->active = $adv->active;
				$id_adv = $this->adv->insert();
								
			}
			//print_r($_POST); echo '<br>Check: ' . ($this->input->post('active'));	
			//redirect('admin/adv/view/');
            redirect('admin/adv');
		}
		
	}
	
	function change_order(){
		$id1 = (int)$this->uri->segment(4);
		$id2 = (int)$this->uri->segment(5);
		
		$adv1 = $this->adv->getById($id1);
		$adv2 = $this->adv->getById($id2);
		
		if($adv1 && $adv2){
			$position1 = $adv1->order_view;
			$adv1->order_view = $adv2->order_view;
			$adv2->order_view = $position1;
			
			$this->adv->update($adv1);
			$this->adv->update($adv2);
			
		}
		
		redirect('admin/adv/');
	}

		
		function upload_image($field_name, $path)
		{
			$result = array();
			echo $config['upload_path'] = $path; //'./Data/Images/';
			$config['allowed_types'] = 'gif|jpg|png|swf|jpeg';
			//$config['max_size']	= '1000';
			//$config['max_width']  = '1024';
			//$config['max_height']  = '768';
			$config['overwrite']  = true;
			
			
			$this->load->library('upload', $config);
		
			if (!$this->upload->do_upload($field_name))
			{
				//$error = array('error' => $this->upload->display_errors());
				$result['error'] = true;
				$result['message'] = $this->upload->display_errors();
				//$this->load->view('upload_form', $error);
			}	
			else
			{
				//$data = array('upload_data' => $this->upload->data());
				$result['error'] = false;
				$result['upload_data'] = $this->upload->data();
			}
			
			return $result;
		}	
		
				
		function friendly_url_check($url){
			$result = $this->news->getByFriendlyUrl($url);
			if ($result!=null){ 
				$this->form_validation->set_message('friendly_url_check', '%s is not available.');
				return FALSE;
			}
			else{
				return TRUE;
			}
		}
		
				
		function testGetFile(){
 		$config = array(
 			'allowed' => array('JPEG', 'jpg', 'PNG', 'png', 'GIF', 'gif'),
 			'max_size'=> 1024000000 /*1Mb*/
 		);
 		
 		 $result = getFile($config, 'Data', 'http://images8.dantri.com.vn/ThumbImages/Uploaded/2009/05/08/lut080520094_327.jpg');
 		 
 		 if(!$result['error']) echo 'Get file success.';
 		 else echo $result['message'];
 		 
 	}
 	
 	function delete(){
 		$id = intval($this->uri->segment(4));
		$ids = array();
 		if($id>0){
			$ids[] = (int)$id;
 			//$this->product->delete($id);
		}
 		else{
 			if(($this->input->post('submitDelcategory'))){
 				$ids = $this->input->post('categoryBox');
 			}
 		}
 		
		if(count($ids)){
	
 			$result = $this->adv->delete($ids);
						
		}
		
 		redirect($_SESSION['his_adm']);
 	}
 	
	}
?>
